package cn.droidlover.xdroidmvp.base;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.lcodecore.tkrefreshlayout.footer.LoadingView;
import com.lcodecore.tkrefreshlayout.header.SinaRefreshView;

import java.util.List;

import cn.droidlover.xdroidmvp.R;
import cn.droidlover.xdroidmvp.kit.Kits;
import cn.droidlover.xdroidmvp.mvp.IPresent;
import cn.droidlover.xdroidmvp.mvp.XFragment;
import cn.droidlover.xdroidmvp.view.DividerItemDecoration;

/**
 * Created by gwy on 2017/11/21.
 */

public abstract class BaseListFragment<T, P extends IPresent> extends XFragment<P> implements ListViewItemClickListener, ListViewChildClickListener {

    protected int mStartIndex = 1;
    protected int total;
    protected int per_page;
    protected int current_page;
    protected int last_page;


    protected boolean isRefresh;
    protected RecyclerView mRecyclerview;
    protected TwinklingRefreshLayout mRefreshLayout;
    protected BaseRecyclerAdapter<T> mBaseAdapter;
    public DividerItemDecoration dividerItemDecoration;

    @Override
    public int getContentViewId() {
        return R.layout.activity_list;
    }

    @Override
    public void initView() {
        mRecyclerview = mainView.findViewById(R.id.recyclerview);
        initRecyclerView(rootView);
        setAdapter();
        initRefreshLayout(rootView);
        mainView = mRecyclerview;
        super.initView();
        if (null != mRefreshLayout) {
            mainView = mRefreshLayout;
        } else {
            mainView = mRecyclerview;
        }
    }

    public void initRecyclerView(View mainView) {
        mRecyclerview = mainView.findViewById(R.id.recyclerview);
        initRecyclerView(mRecyclerview);
    }

    public void initRecyclerView(RecyclerView recyclerView) {
        final LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        dividerItemDecoration = new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST);
        recyclerView.addItemDecoration(dividerItemDecoration);
        recyclerView.setLayoutManager(layoutManager);
    }

    private void setAdapter() {
        mBaseAdapter = getAdapter();
        mRecyclerview.setAdapter(mBaseAdapter);
        mBaseAdapter.setItemClickListener(this);
        mBaseAdapter.setChildClickListener(this);
    }

    public void setAdapter(RecyclerView recyclerView) {
        BaseRecyclerAdapter baseAdapter = getAdapter();
        recyclerView.setAdapter(baseAdapter);
        baseAdapter.setItemClickListener(this);
        baseAdapter.setChildClickListener(this);
    }

    @Override
    public void onFail(String message) {
        super.onFail(message);
        twinkCompleted();
    }

    public void onSuccess(List<T> list) {

        mBaseAdapter.addList(isRefresh, list);
        if (!Kits.Empty.check(list)) {
            mStartIndex++;
        }
        if (0 == mBaseAdapter.getmList().size()) {
            showNoDataPage();
        } else {
            showMainView();
        }
        twinkCompleted();
        disLoadDialog();
    }

    public void onSuccess(boolean isRefresh, List<T> list) {
        mBaseAdapter.addList(isRefresh, list);
        if (!Kits.Empty.check(list)) {
            mStartIndex++;
        }
        if (0 == mBaseAdapter.getmList().size()) {
            showNoDataPage();
        } else {
            showMainView();
        }
        twinkCompleted();
        disLoadDialog();
    }

    @Override
    public boolean showNoDataPage() {
        if (0 == mBaseAdapter.getmList().size() || isRefresh) {
            return super.showNoDataPage();
        } else {
            showMainView();
        }
        return false;
    }

    @Override
    public void twinkCompleted() {
        if (mRefreshLayout != null) {
            if (isRefresh) {
                mRefreshLayout.finishRefreshing();
            } else {
                mRefreshLayout.finishLoadmore();
            }
        }
    }


    public void initRefreshLayout(View mainView) {
        mRefreshLayout = mainView.findViewById(R.id.refresh);
        if (mRefreshLayout == null) {
            return;
        }
        SinaRefreshView headerView = new SinaRefreshView(getActivity());
        mRefreshLayout.setHeaderView(headerView);
        mRefreshLayout.setMaxHeadHeight(140);
        mRefreshLayout.setOverScrollBottomShow(false);
        mRefreshLayout.setOnRefreshListener(new RefreshListenerAdapter() {
            @Override
            public void onRefresh(final TwinklingRefreshLayout refreshLayout) {
                isRefresh = true;
                getListDatas(true);
            }

            @Override
            public void onLoadMore(final TwinklingRefreshLayout refreshLayout) {
                isRefresh = false;
                getListDatas(false);
            }
        });
        LoadingView loadingView = new LoadingView(getActivity());
        mRefreshLayout.setBottomView(loadingView);
    }


    public abstract BaseRecyclerAdapter<T> getAdapter();

    public void getListDatas(boolean isRefresh) {
        this.isRefresh = isRefresh;
        if (isRefresh) {
            mStartIndex = 1;
            per_page = 0;
        }
    }


    @Override
    public void itemChildClick(int position, int viewId) {

    }

    @Override
    public void loadFail() {
        twinkCompleted();
    }

    public void onSuccess(int total, int per_page, int current_page, int last_page, List<T> list) {
        disLoadDialog();
        this.total = total;
        this.per_page = per_page;
        this.current_page = current_page;
        this.last_page = last_page;
        mBaseAdapter.addList(isRefresh, list);
        twinkCompleted();
    }
}
